package com.gujiangbo.application.utils;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.jdt.internal.compiler.batch.Main;

import com.gujiangbo.application.exception.IMPException;

/**
 * 文件操作工具类
 * 
 * @author Administrator
 *
 */
public class FileHelper {

	private static Log log = LogFactory.getLog(FileHelper.class);
	private static final String ENCODING = "UTF-8";
	private static final int TO_READ_LENGTH = 10000;

	/**
	 * 获取目录下所有的文件名称
	 * 
	 * @param filePath
	 * @return
	 * @throws IMPException
	 */
	public static List<String> getfileName(String filePath) throws IMPException {
		List<String> filelist = new ArrayList<>();
		if (StringUtils.isNotBlank(filePath)) {
			File file = new File(filePath);
			if (file.exists()) {
				File[] listFiles = file.listFiles();
				if (listFiles != null) {
					for (File child : listFiles) {
						if (child.isFile()) {
							filelist.add(child.getName());
						} else if (child.isDirectory()) {
							filelist.addAll(getfileName(child.getPath()));
						}
					}
				}
			}
		}
		return filelist;
	}

	public static String readFromStream(InputStream in, int len) throws IMPException {
		String temp = null;
		try {
			if (len > 0) {
				byte[] b = new byte[len];
				int off = in.read(b);
				int location = off;
				while ((location < len) && (off > 0)) {
					off = in.read(b, location, len - location);
					if (off < 0) {
						break;
					}
					location += off;
				}
				temp = new String(b, "UTF-8");
			}
		} catch (Exception ex) {
			throw new IMPException(
					"Read data from InputStream , catch a Exception , exception message : " + ex.getMessage(), ex);
		}
		return temp;
	}

	public static void main(String[] args) {
		try {
			List<String> fileName = getfileName("E:\\数据库脚本");
			for (String child : fileName) {
				System.out.println(child);
			}
		} catch (IMPException e) {
			e.printStackTrace();
		}
	}
}
